const Base = require('./base.js');

module.exports = class extends Base {
  async updateAction() {
    const user = this.model('user');
    const user_level = await user
      .where({
        id: this.getLoginUserId()
        //   user_level_id: 2
      })
      .getField('user_level_id', true);
    const rate = await this.model('user_level')
      .where({ id: user_level })
      .getField('credit_rate', true);
    const data = await user
      .where({
        id: this.getLoginUserId()
      })
      .increment({
        credit: this.post('credit') * rate,
        spend_total: this.post('credit')
      });
    const spend_total = await user
      .where({
        id: this.getLoginUserId()
      })
      .getField('spend_total', true);
    const result = await user
      .where({
        id: this.getLoginUserId()
      })
      .update({
        user_level_id:
          user_level != 2 && spend_total > 100 ? 2 : ['exp', 'user_level_id+0']
      });
    return this.success(result);
  }
};
